package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import com.mbc.common.entities.LevelApplicationItem;

public class LevelApplicationItemDAOImpls extends DAO<LevelApplicationItem> implements LevelApplicationItemDAO {

	public LevelApplicationItemDAOImpls(Connection conn)
	{
		this.conn = conn;
		this.table_name = "LevelApplicationItem";
	}

	@Override
	public void create(LevelApplicationItem entity) throws SQLException {
		query = "insert into " + table_name + " ( roleid, appitemid, accesscodes) values( ?, ?, ?)";
		pstmt = conn.prepareStatement(query);
		int i = 1;
		pstmt.setLong(i++, entity.getRoleId());
		pstmt.setLong(i++, entity.getAppitemId());
		pstmt.setString(i++, entity.getAccessCodes());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	protected LevelApplicationItem readRecord(ResultSet rs) throws SQLException {
		LevelApplicationItem l_entity = new LevelApplicationItem();
		l_entity.setAutokey(rs.getLong("autokey"));
		l_entity.setRoleId(rs.getLong("roleid"));
		l_entity.setAppitemId(rs.getLong("appitemid"));
		l_entity.setAccessCodes(rs.getString("accesscodes"));
		return l_entity;
	}

	@Override
	public void deleteByRoleId(long roleId) throws SQLException {
		query = getDeleteQuery() + " where roleid = " + roleId;
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public ArrayList<LevelApplicationItem> findByRoleId(long roleId)
			throws SQLException {
		ArrayList<LevelApplicationItem> l_LevelAppitemList = new ArrayList<LevelApplicationItem>();
		query = getSelectQuery() + " where roleid = " + roleId;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		while (rs.next()) {
			l_LevelAppitemList.add(readRecord(rs));
		}
		rs.close();
		pstmt.close();
		return l_LevelAppitemList;
	}
}
